<?php

defined('SYSPATH') or die('Access error!');

/**
 * 供求平台的ajax类
 * ============================================================================
 * 功能描述： 
 *      用户的初始化以及常用的一些方法
 * ============================================================================
 * $Author: liuxudong $
 */
class Controller_Trade_Ajax extends Controller_Ajax
{

    public function before()
    {
        parent::before();
        I18n::set_current_file('trade');
    }

    public function action_add_view()
    {
        $id = Arr::get($_POST, 'id', '');
        if (empty($id))
        {
            exit;
        }
        $view_count = Model::factory('trade_post')->get_var(array('id' => $id), 'view_count');
        if (empty($view_count))
        {
            exit;
        }
        Model::factory('trade_post')->update(array('view_count' => $view_count + 1), array('id' => $id));
    }

    public function action_order_post()
    {
        $category_id = Arr::get($_POST, 'category_id', SYSTEM_TRADE_CATEGORY);
        $post_type = Arr::get($_POST, 'post_type', '');
        $order_type = Arr::get($_POST, 'order_type', '');
        $order_con = Arr::get($_POST, 'order_con', '');
        $page_num = Arr::get($_POST, 'page_num', 1);
        $_GET['page'] = $page_num;

        $order_sql = array();
        $order_field = $this->handle_order_field($order_type);
        $order_sql[$order_field] = $order_con;

        $data = array();

        if ($category_id != SYSTEM_TRADE_CATEGORY)
        {
            //获取分类的所有子分类
            $childs = $this->get_category_childs($category_id, 'trade');
            $data['category_id'] = !empty($childs) ? $childs : $category_id;
        }

        if ($post_type != 'list')
        {
            $data['post_type'] = $post_type;
        }


        $trade_list = Model::factory('trade_post')->get_trade_list($data, 10, $order_sql);

        echo json_encode($trade_list['list']);
    }

    public function handle_order_field($order_type)
    {
        switch ($order_type)
        {
            case 'time':
                return 'add_time';
                break;
            case 'price':
                return 'account';
                break;
            case 'view':
                return 'view_count';
                break;
            case 'commont':
                return 'comment_count';
                break;
            case 'limit':
                return 'limit_time';
                break;
            default :
                break;
        }
    }

    //获取某个供求分类的内容模板
    public function action_category_template()
    {
        $category_id = Arr::get($_POST, 'id', '');
        if (empty($category_id))
        {
            echo 1;
            exit;
        }
        $content = Model::factory('trade_template')->get_var(array('category_id' => $category_id), 'content');
        if (!empty($content))
        {
            echo $content;
            exit;
        }
    }

}